package com.jojo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jojo.entity.charts.*;
import com.jojo.mapper.charts.*;
import com.jojo.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    Chart1Mapper chart1Mapper;
    @Autowired
    Chart2Mapper chart2Mapper;
    @Autowired
    Chart3Mapper chart3Mapper;
    @Autowired
    Chart4Mapper chart4Mapper;
    @Autowired
    Chart5Mapper chart5Mapper;
    @Autowired
    Chart6Mapper chart6Mapper;


    @Override
    public List<Chart1> getChart1() {
        return chart1Mapper.selectList(null);
    }

    @Override
    public List<Chart2> getChart2() {
        return chart2Mapper.selectList(null);
    }

    @Override
    public List<Chart3> getChart3() {
        return chart3Mapper.selectList(null);
    }

    @Override
    public List<Chart4> getChart4(){
        return chart4Mapper.selectList(null);
    }

    @Override
    public JSONArray getChart5() {
        List<Chart5> chart5List= chart5Mapper.selectList(null);
        List<Double> list;
        JSONArray jsonArray = new JSONArray();
        for(Chart5 chart5:chart5List){
            list = new ArrayList<>();
            list.add(chart5.getAvgHeartRate());
            list.add(chart5.getAvgQualityOfSleep());
            list.add(chart5.getAvgPhysicalActivityLevel());
            list.add(chart5.getAvgStressLevel());
            list.add(chart5.getAvgSleepDuration());
            JSONObject object = new JSONObject();
            object.put("name",chart5.getAgeGroup());
            object.put("value",list);
            jsonArray.add(object);
        }
        return jsonArray;
    }

    @Override
    public List<Chart6> getChart6() {
        return chart6Mapper.selectList(null);
    }

}
